#include <iostream>
using namespace std;

class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    virtual ~Person()
    {
        cout << "~Person()" << endl;
    }

    virtual void run()
    {
        cout << "Person::run()" << endl;
    }

private:
    int m_age;

};

class Student : public Person
{
public:
    Student()
    {
        cout << "Student()" << endl;
    }
    ~Student()
    {
        cout << "~Student()" << endl;
    }

    virtual void run()
    {   
        Person::run();
        cout << "Student::run()" << endl;
    }

private:
    int m_score;

};

int main()
{
    // Person * p1 = new Student();
    // p1->run();
    // delete p1;

    Person * p1 = new Person();
    Person * p2 = new Student();
    /* 错误示范*/
    // Student * stu1 = p1;
    /* 内存泄漏:
            1, 野指针;
            2，malloc分配没有释放
            3，踩内存(其中子类指针指向父类没有用dynamic_cast 或许会内存泄漏)
    */
    // Student * stu2 = p2;
    /* 解决方法1*/
    // Student * stu1 = (Student *)p1;
    // Student * stu2 = (Student *)p2;
    /* 解决方法2, 有安全检测*/
    Student * stu1 = dynamic_cast<Student *> (p1);
    Student * stu2 = dynamic_cast<Student *> (p2);


    return 0;
}